<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\AdHandler;
use app\common\logic\NewsApi;
use app\common\model\cast\Category;
use app\common\model\cast\Item;
use think\Db;

/**
 * 栏目
 */
class Cast extends Api
{
    protected $noNeedLogin = ['getCate', 'news', 'getNav', 'getAppItems', 'rank','getAllItems'];
    protected $noNeedRight = ['*'];


    public function getNav() {
        //获取重组栏目
        $nav1 = Item::where(['switch'=> 1, 'type'=> 0])->whereOr(['navswitch'=> 1])->order('weigh asc')->select();

        //添加的栏目
        $nav2 = [];
        if (defined('UID') && UID) {
            $nav2 = Item::where(['switch'=> 1, 'type'=> 1])->where('id', 'in', function($query) {
                $query->name('self_my')->where(['user_id'=> UID])->field('item_id');
            })->select() ;
            $nav2 = collection($nav2)->toArray();
        }

        $nav1 = collection($nav1)->toArray();
        $navs = array_merge($nav1, $nav2);

        $this->success('导航', $navs);
    }

    public function addItem() {
        $item_id = input('item_id/d', 0);
        if (!$item_id) {
            $this->error('参数传递错误');
        }

        Db::name('self_my')->insert([
            'item_id' => $item_id,
            'user_id' => $this->auth->id
        ]);

        $this->success('添加栏目成功');
    }

    //获取添加栏目列表
    public function getCate() {
        $cate = new Category();
        $list = $cate->order('weigh asc')->select();
        $this->success('栏目列表', $list);
    }

    //获取分类 下面的栏目
    public function getItems() {
        $cate = input('cate_id/d', 0);
        if (!$cate) {
            $this->error('参数错误');
        }
        $list = Item::where(['switch'=> 1, 'type'=> 1, 'navswitch'=> 0])->where('id', 'in', function($query) use ($cate) {
            $query->name('self_cate_to_item')->where(['category_id'=> $cate])->field('item_id');
        })->select();

        foreach ($list as $k => &$v) {
            $v->is_my = Db::name('self_my')->where(['user_id'=> $this->auth->id, 'item_id'=> $v->id])->count(); //0为非， 1为是
        }

        $this->success('分类下面栏目', $list);
    }

    //app专用接口
    public function getAppItems() {
        $cate = input('cate_id/d', 0);
        if (!$cate) {
            $this->error('参数错误');
        }
        $list = Item::where(['switch'=> 1, 'type'=> 1, 'navswitch'=> 0])->where('id', 'in', function($query) use ($cate) {
            $query->name('self_cate_to_item')->where(['category_id'=> $cate])->field('item_id');
        })->select();


        $this->success('分类下面栏目', $list);
    }


     //pw by losan 2020.5.26
    public function getAllItems() {
        $item = new Item();
        $items = $item->where('type', 0)->field('tag as id, name')->select();
        $this->success('全部频道', $items);
    }

    public function news() {
        $tag = input('tag/s', '');
        $adrow = input('adrow/d', 1);
        $news = (new NewsApi($tag))->getList();
        if ($adrow > 1) {
            $ad = AdHandler::getRandMany($tag);
        } else {
            $ad = AdHandler::getRandAds($tag);
        }
		
		//pw by  losan  2020.5.27
		$ad1 = null;
		$iaa = file_get_contents("http://e.aebrowser.cn/index.php/publicapi/getVipGd?page=".input('page/d', 1).'&pd_id='.$tag);	
          if($iaa)
		  {
			 $iaa = json_decode($iaa,true);
			 if($iaa['status']==1)
			 {
				$iaa = $iaa['data'];
					 $ad1 = [
			 'keyword'=>$iaa['name'],
			 'name'=>$iaa['video']?$iaa['video']:$iaa['biaoshi'],
			 'src_text'=>$iaa['src'],
			 'title'=>$iaa['longtitle'],
			  'linkurl'=>$iaa['url'],
			   "target"=> "_blank",
			   "desc_text"=> $iaa['miaoshu']
			 ]; 
			 }
		  }
			
        $this->success('news', [
            'ad' => $ad1,
            'news' => $news,
            'update' => input('row/d', 15)
        ]);
    }


    /**
     * 首页接口
     * @param int $page
     */
    public function rank($limit = 8) {
        $hot = Db::name('self_news')->whereTime('createtime','-7 day')->order('viewCount desc')->limit($limit)->select();
        $Adszone = new \addons\adszone\library\Adszone();
        $result = $Adszone->getAdsByMark('rankpics'); //按照标记调用广告位
        $data = $result['data'];
        foreach ($data as $k => &$v) {
            $v['imageurl'] = cdnurl($v['imageurl'], true);
        }

        $result['data'] = $data;

        $this->success('排行榜', [
            'hot' => $hot,
            'banner'  => $result
        ]);
    }
    
}

